﻿using System;
using System.Security.Permissions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace AIStudio.Wpf.ECharts.Controls
{
    /// <summary>
    /// Echart.xaml 的交互逻辑
    /// </summary>
    public partial class GaugeEchart : UserControl
    {

        public static readonly DependencyProperty DataProperty = DependencyProperty.Register(
                  "Data", typeof(GangeValueClass), typeof(GaugeEchart), new PropertyMetadata(null, OnValuesCollectionPropertyCallback));
        public GangeValueClass Data
        {
            get { return (GangeValueClass)GetValue(DataProperty); }
            set { SetValue(DataProperty, value); }
        }

        private static void OnValuesCollectionPropertyCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var chart = (GaugeEchart)d;
            chart.OnValuesCollectionPropertyCallback(e.NewValue as GangeValueClass, e.OldValue as GangeValueClass);
        }

        private void OnValuesCollectionPropertyCallback(GangeValueClass newvalue, GangeValueClass oldvalue)
        {
            if (oldvalue != null)
            {
                oldvalue.PropertyChanged -= Newvalue_PropertyChanged;
            }
            if (newvalue != null)
            {
                newvalue.PropertyChanged += Newvalue_PropertyChanged;
            }
            DrawGauge();
        }

        private void Newvalue_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Value")
            {
                Web.InvokeScript("jsSetValue", Data.Value);
            }
            else
            {
                DrawGauge();
            }
        }
        

        public GaugeEchart()
        {
            InitializeComponent();
            Web.ObjectForScripting = new WebAdapter(Web);
            Web.Navigate(new Uri(System.AppDomain.CurrentDomain.BaseDirectory + "Htmls\\GaugeChart.html"));
            SizeChanged += GaugeEchart_SizeChanged;

            this.Loaded += GaugeEchart_Loaded;
        }

        private void GaugeEchart_Loaded(object sender, RoutedEventArgs e)
        {
            Task.Run(async delegate
            {
                await Task.Delay(1000);

                Dispatcher.Invoke(() => DrawGauge());
            });

        }

        private void GaugeEchart_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (!IsLoaded) return;

            try
            {
                Web.InvokeScript("jsSetSize", (int)(this.ActualWidth * 0.97), (int)(this.ActualHeight * 0.97));
                DrawGauge();
            }
            catch { }
        }

        private void DrawGauge()
        {
            try
            {
                if (!IsLoaded) return;

                if (Data != null)
                {
                    Web.InvokeScript("jsSetData",Data.Name, Data.Min, Data.Max, Data.SplitNumber, Data.Label, Data.Value );
                }
            }
            catch (Exception ex)
            {

            }
        }


     
        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        [System.Runtime.InteropServices.ComVisible(true)]//给予权限并设置可见
        public class WebAdapter
        {
            WebBrowser web;
            public WebAdapter(WebBrowser web)
            {
                this.web = web;
            }
            public void ShowMsg(string Msg)
            {
                MessageBox.Show(Msg);
            }
        }
    }


    public class GangeValueClass : BindableBase
    {
        private string _name = "Gauge";
        public string Name
        {
            get { return _name; }
            set
            {
                if (_name != value)
                {
                    SetProperty(ref _name, value);
                }
            }
        }

        private double _max = 100;
        public double Max
        {
            get { return _max; }
            set
            {
                if (_max != value)
                {
                    SetProperty(ref _max, value);
                }
            }
        }

        private double _min = 0;
        public double Min
        {
            get { return _min; }
            set
            {
                if (_min != value)
                {
                    SetProperty(ref _min, value);
                }
            }
        }

        private int _splitNumber;
        public int SplitNumber
        {
            get { return _splitNumber; }
            set
            {
                if (_splitNumber != value)
                {
                    SetProperty(ref _splitNumber, value);
                }
            }
        }       

        private double _value;
        public double Value
        {
            get { return _value; }
            set
            {
                if (_value != value)
                {
                    SetProperty(ref _value, value);
                }
            }
        }

        private string _label = "";
        public string Label
        {
            get { return _label; }
            set
            {
                if (_label != value)
                {
                    SetProperty(ref _label, value);
                }
            }
        }
    }
}
